<template>
  <div>
    <van-nav-bar
      title="修改地址"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-address-edit
      :area-list="areaList"
      :show-postal="false"
      show-delete
      :address-info="addre"
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
      @change-detail="onChangeDetail"
    />
  </div>
</template>

<script>
import { Toast } from "vant";
import { Dialog } from "vant";
import { areaList } from "@vant/area-data";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      areaList,
      searchResult: [],
      addre: {},
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },

    onSave(item) {
      let adress = item.province + item.addressDetail;
      let adre = {
        id: item.id,
        name: item.name,
        tel: item.tel,
        address: adress,
        isDefault: item.isDefault,
      };
      this.$store.dispatch("addAdress", adre);
      Dialog.alert({
        message: "保存成功",
        theme: "round-button",
      }).then(() => {
        // on close
        this.addre = {};
      });
      // console.log(addressList);
    },
    onDelete(item) {
      this.$store.dispatch("delAddress", item.id);
      Dialog.alert({
        message: "删除成功",
        theme: "round-button",
      }).then(() => {
        // on close
        this.addre = {};
      });
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: "黄龙万科中心",
            address: "杭州市西湖区",
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
  },
  computed: {
    ...mapGetters(["addressList"]),
  },
  created() {
    let index = this.$route.query.index;
    this.addre = this.addressList[index];
  },
};
</script>

<style>
</style>